﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoreData;
using EntityData;
using Helper;
using Services.BusinessRules;
namespace Services
{

    public partial class AttributeService
    {
        readonly IAttributeEntity entity;
        readonly IAuditEntity AuditEntity;
        public AttributeService()
        {
            this.entity = new AttributeEntity();
            this.AuditEntity = new AuditEntity();
        }

        #region Main Method

        /// <summary>
        /// Save 
        /// </summary>
        /// <param name="_model"></param>
        /// <returns></returns>
        public long Save(object _model, ref string mess)
        {
            try
            {

                if (Validate((CoreData.Attribute)_model, ref mess))
                {
                    Audit audit = new Audit();
                    audit.ModifiedDate = DateTime.Now;
                    audit.UserID = ValueDefine.UserID;
                    audit.Module = "Thuộc tính sản phẩm";
                    audit.Code = "Attribute";
                    var id = long.Parse(_model.GetType().GetProperty("ID").GetValue(_model, null).ToString());
                    if (id == 0)
                    {
                        audit.Action = "Thêm mới";
                        audit.Description = Diff(new CoreData.Attribute(), (CoreData.Attribute)_model);
                        AuditEntity.Save(audit, Table.Audit.ToString());
                        return entity.Save((CoreData.Attribute)_model, Table.Attribute.ToString());
                    }
                    else
                    {
                        audit.Action = "Cập nhật";
                        audit.Description = Diff(GetByID(id), (CoreData.Attribute)_model);
                        AuditEntity.Save(audit, Table.Audit.ToString());
                        return entity.Update((CoreData.Attribute)_model, Table.Attribute.ToString());
                    }
                }
                else
                { return -1; }
            }
            catch { return -1; }
        }

        /// <summary>
        /// Get by ID
        /// </summary>
        /// <param name="_id"></param>
        /// <returns></returns>
        public CoreData.Attribute GetByID(long _id)
        {
            try
            {
                return entity.GetById(_id, Table.Attribute.ToString());
            }
            catch { return null; }
        }

        /// <summary>
        /// Get List
        /// </summary>
        /// <returns></returns>
        public IList<CoreData.Attribute> GetList()
        {
            try
            {
                return entity.GetAll(Table.Attribute.ToString()).ToList();
            }
            catch { return null; }

        }

        /// <summary>
        /// Get List with Linq
        /// </summary>
        /// <returns></returns>
        public IList<CoreData.Attribute> GetListByLINQ(Func<CoreData.Attribute, Boolean> _where)
        {
            try
            {
                return entity.GetMany(_where, Table.Attribute.ToString()).ToList();
            }
            catch { return null; }
        }
        /// <summary>
        /// Get One with Linq
        /// </summary>
        /// <returns></returns>
        public CoreData.Attribute GetOneByLINQ(Func<CoreData.Attribute, Boolean> _where)
        {
            try
            {
                return entity.Get(_where, Table.Attribute.ToString());
            }
            catch { return null; }
        }

        /// <summary>
        /// Get List with search string
        /// </summary>
        /// <returns></returns>
        public IList<CoreData.Attribute> GetList(string _searchstring)
        {
            //search theo tieu chi nao do         
            try
            {
                return entity.GetBySearchString(_searchstring, Table.Attribute.ToString()).ToList();
            }
            catch { return null; }
        }


        /// <summary>
        /// Delete
        /// </summary>
        /// <param name="_model"></param>
        /// <returns></returns>
        public bool Delete(object _model)
        {
            try
            {
                entity.Delete((CoreData.Attribute)_model, Table.Attribute.ToString());

                Audit audit = new Audit();
                audit.ModifiedDate = DateTime.Now;
                audit.UserID = ValueDefine.UserID;
                audit.Module = "Thuộc tính hàng hóa";
                audit.Code = "Attribute";
                audit.Action = "Xóa";
                audit.Description = " Xóa thuộc tính: " + ((CoreData.Attribute)_model).Name;
                AuditEntity.Save(audit, Table.Audit.ToString());

                return true;
            }
            catch { return false; }
        }


        #endregion



        #region Other Method

        bool Validate(CoreData.Attribute obj, ref string mess)
        {
            new ValidateRequired("Mã thuộc tính", "Code", obj.Code);
            new ValidateRequired("Tên thuộc tính", "Name", obj.Name);
            new ValidateExist("Mã thuộc tính", "Code", "Code", obj, GetList().Cast<object>().ToList());
            new ValidateLenght("Mã thuộc tính", "Code", obj.Code.ToString(), 25);
            new ValidateLenght("Tên thuộc tính", "Code", obj.Name.ToString(), 25);
            mess = BusinessObject.Validate();
            BusinessObject.Clear();
            return mess == null ? true : false;
        }


        string Diff(CoreData.Attribute OldItem, CoreData.Attribute NewItem)
        {
            string content = "";
            if (NewItem.ID == 0)
            {
                content = "Thêm mới một thuộc tính sản phẩm: " + NewItem.Name;
            }
            else
            {
                if (OldItem.Code != NewItem.Code)
                {
                    content += "- Mã: " + OldItem.Code + " --> " + NewItem.Code + "\r\n";
                }
                if (OldItem.Name != NewItem.Name)
                {
                    content += "- Tên: " + OldItem.Name + " --> " + NewItem.Name + "\r\n";
                }
                if (OldItem.Description != NewItem.Description)
                {
                    content += "- Mô tả: " + OldItem.Description + " --> " + NewItem.Description + "\r\n";
                }
                if (OldItem.Active != NewItem.Active)
                {
                    content += "- Active: " + OldItem.Active + " --> " + NewItem.Active + "\r\n";
                }
                
            }
            return content;
        }



        #endregion

    }

}
